//
//  WTComposeTextView.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/7/3.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit

class WTComposeTextView: UITextView {
  
    // 重写父类已有属性
    override var font: UIFont?{
      // 通过didSet监听font改变
        didSet{
        
          placeholderLabel.font = font
        
        }
    }
     // 站位文字
    var placeholder: String?{
    
        didSet{
        
          placeholderLabel.text = placeholder
        
        }
    
    
    }
    
    
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setUpUI(){
    
     backgroundColor = UIColor.whiteColor()
      // 添加控件
        addSubview(placeholderLabel)
         // 添加约束
     placeholderLabel.snp_makeConstraints { (make) in
        
         make.top.equalTo(self).offset(8)
         make.left.equalTo(self).offset(5)
         make.width.equalTo(kScreenWidth - 10)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "TextViewTextDidChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
     // 监听方法
    func TextViewTextDidChange()  {
        
        placeholderLabel.hidden = self.hasText()
        
        
    }

    // 移除通知
    deinit{
    
     NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel:UILabel = {
    
     let label = UILabel()
     label.textColor = UIColor.grayColor()
     label.font = UIFont.systemFontOfSize(12)
     label.numberOfLines = 0
        
      return label
    
    }()
    
}
